package 复杂的成员初始化_static;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class Student {
	String name;
	int age;
	Student()
	{
		
		
	}
	Student(String name,int age)
	{
		this.name=name;
		this.age=age;
	}
	void printInfo()
	{
		System.out.println(name+"\t"+age);
		
	}
}
	//年龄比较器
	class AgeComparator implements Comparator<Student>
	{
		public int compare(Student o1,Student o2)
		{
			return o1.age-o2.age;
		}
	}

public class ComparatorDemo {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Student s1=new Student("Jacky",21);
		Student s2=new Student("Frank",20);
		Student s3=new Student("Annie",23);
		List<Student> a1=new ArrayList<Student>();
		a1.add(s1);
		a1.add(s2);
		a1.add(s3);
		//按年龄排序
		AgeComparator nc2=new AgeComparator();
		Collections.sort(a1,nc2);
		System.out.println("按年龄排序");
		for(Student o:a1)
		{
			o.printInfo();
		}
		//按姓名排序，创建匿名类
		Collections.sort(a1,new Comparator<Student>()
		
		{

			@Override
			public int compare(Student o1, Student o2) {
				// TODO Auto-generated method stub
				return o1.name.compareTo(o2.name);
			}
			
		});
		//打印
		System.out.println("按姓名排序");
		for (Student o:a1)
		{
			o.printInfo();
		}
		a1.sort((e1,e2)->{return e1.name.compareTo(e2.name);});
		//打印
		System.out.println("按姓名排序");
		a1.forEach(e->e.printInfo());
	}

}